import * as THREE from "three";
import CimEntity from './CimEntity'
import CimBuilding from "./CimBuilding";
import CimOther from "./CimOther";
class CimScene {
    constructor(scene_id, config) {
        this.scene_id = scene_id
        this.createdTime = new Date().toLocaleDateString() + ' ' + new Date().toLocaleTimeString()
        this.lod = config.lodsetting
        this.background = config.default_background
        this.cameraSet = config.default_camera
        this.children = []
        this.cimStack = []
        this.cimStack.push({ header: config, cim: this })
    }

    createScene(width, height) {
        //创建THREE场景
        let scene = new THREE.Scene()
        let camera = this.createCamera(this.cameraSet[0], width, height)
        let background = this.createBackGround(this.background, width, height)
        let axes = new THREE.AxesHelper(3000);
        //渲染器不应该在这里构建，后续修改！！！
        let renderer = background.renderer
        scene.add(camera)
        scene.add(background.light)
        scene.add(background.directionalLight)
        scene.add(axes)
        return { scene, camera, renderer }
    }

    createCamera(config, width, height) {
        //创建THREE相机
        let camera = new THREE.PerspectiveCamera(config.pov, width / height, 0.01, config.depth);
        camera.position.set(config.position.x, config.position.y, config.position.z); //设置相机位置
        camera.lookAt(config.view_direction.x, config.view_direction.y, config.view_direction.z); //设置相机方向(指向的场景对象)
        return camera
    }

    createBackGround(config, width, height) {
        //创建THREE背景
        let light = new THREE.AmbientLight(config.ambientLight);
        let directionalLight = new THREE.DirectionalLight(config.sunlight.color, config.sunlight.denity);
        directionalLight.position.set(config.sunlight.position.x, config.sunlight.position.y, config.sunlight.position.z);

        let renderer = new THREE.WebGLRenderer({
            antialias: true,
            logarithmicDepthBuffer: true,
        });
        renderer.setSize(width, height); //设置渲染区域尺寸
        renderer.setClearColor(new THREE.Color(config.color), config.colorAlpha); //设置背景颜色
        renderer.domElement.id = "renderer-canvas";

        if (config.shadow) {
            //设置阴影
        }
        return {
            light, directionalLight, renderer
        }
    }

    loadEntity() {
        //创建子节点数据结构
        while (this.cimStack.length > 0) {
            let h = this.cimStack.shift()
            let cim = h.cim
            let children = h.header.children

            if (children && children.length > 0) {

                children.forEach(child => {
                    if(child.type == "other"){
                        let cimChild = new CimOther(child)
                        cimChild.parent=cim
                        cim.children.push(cimChild)
                        this.cimStack.push({
                            header: child,
                            cim: cimChild
                        })
                    }
                    else if (child.children) {
                        let cimChild = new CimEntity(child)
                        cimChild.parent=cim
                        cim.children.push(cimChild)
                        this.cimStack.push({
                            header: child,
                            cim: cimChild
                        })
                    } else {
                        child.lodSetting=this.lod
                        let cimChild = new CimBuilding(child)
                        cimChild.parent=cim 
                        cim.children.push(cimChild)
                        this.cimStack.push({
                            header: child,
                            cim: cimChild
                        })
                    }

                })
            } else if (!children) {
                //console.log('建筑or环境', cim);
            }
        }

    }




}

CimScene.prototype.test = 1

export default CimScene
